#ifndef INCLUDED_STANZAMANAGER_H
#define INCLUDED_STANZAMANAGER_H
#include "socket.h"
#include "XmlParser.h"
#include "sigslot.h"
#include <queue>
// use sigslot to notify network events

namespace tinkle{

class StanzaManager : public sigslot::has_slots<>
{
public:
	StanzaManager(Socket &sock, XmlParser &parser);
	~StanzaManager();

	//when a  new XMLStanza come in, StanzaManager will notified any class connected to this SigRead.
    sigslot::signal1<StanzaManager*> SigRead;
	//signal0<> SigClose;

	//Add a stanza to send queue. After the stanza send, the stanza will be deleted&released.
	void Write(XmlNode* stanza);

	//Read a Node from StanzaManager read queue. You must release the stanza by yourself
	XmlNode* Read();

	//Clear the read and the write queue. ALL the stanza in read queue will be deleted&released,
	//Stanzas in write queue which is specified to NOT reserve status will be deleted&released.
	void Clear();
	
	//Is this necessary?
	Socket& GetSock() {return _cSocket;}
	
	int EnableTLS();
	int DisableTLS();
	bool IsTLSEnabled();
protected:
	
private:
    std::queue<XmlNode*> _qReadStanzas, _qWriteStanzas;
	//queue<bool> _qReserve;

	Socket &_cSocket;
	XmlParser &_cParser;

	enum {
		BUFFLEN = 4096,
	};
	char _Buf[BUFFLEN];
private:
	
	void _SendStanza(Socket& sock);
	void _recvfromsocket(Socket& sock);
	void _PopWriteQueue();
	static void _XmlNodeCallBack(XmlNode*, void* mgr);
};



}
#endif